﻿using System;
using System.IO;

namespace Microsoft.WowAddonStudio.Project.Import
{
	/// <summary>
	/// Represents an addon item that is opened either from the local filesystem or
	/// local network.
	/// </summary>
	public class FolderAddonItem : AddonItem
	{
		private readonly string absolutePath;

		/// <summary>
		/// Creates an instance of <see cref="FolderAddonItem"./>
		/// </summary>
		/// <param name="rootFolderPath">Root import folder.</param>
		/// <param name="relativePath">Relative path that will be combined with root folder.</param>
		public FolderAddonItem(string rootFolderPath, string relativePath)
			: base(relativePath)
		{
			if (string.IsNullOrEmpty(rootFolderPath))
				throw new ArgumentNullException("rootFolderPath", "Root folder should be set.");

			if (!Directory.Exists(rootFolderPath))
				throw new ArgumentException("Root folder does not exist.", "rootFolderPath");

			absolutePath = Path.Combine(rootFolderPath, relativePath);

			if (!File.Exists(absolutePath))
				throw new ArgumentException("Non-existing file specified as addon item.");
		}

		/// <summary>
		/// Gets the contents of the addon item as a stream. Please note that the obtained 
		/// stream should be disposed by the caller.
		/// </summary>
		/// <returns>An instance of the <see cref="Stream"/> class.</returns>
		public override Stream GetStream()
		{
			return File.Open(absolutePath, FileMode.Open, FileAccess.Read);
		}
	}
}